package com.xujl.mvcbind.window;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.xujl.fastlib.utils.DateUtil;
import com.xujl.mvcbind.R;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 时间选择弹窗
 */
public class DatePickerBottomWindow {
    private TimePickerView pvTime;
    private CompleteListener mCompleteListener;
    private String dateStr;

    public DatePickerBottomWindow (Context context) {
        pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect (Date date, View v) {
                Log.e("DatePickerBottomWindow", "onTimeSelect:" + date.toString());
            }
        })
                .setLayoutRes(R.layout.window_date_picker_bottom, new CustomListener() {
                    @Override
                    public void customLayout (View v) {
                        v.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick (View view) {
                                pvTime.dismiss();
                                if (mCompleteListener != null) {
                                    mCompleteListener.onCompleteClick(dateStr);
                                }
                            }
                        });
                    }
                })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged (Date date) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        dateStr = DateUtil.getTime(format.format(date), "yyyy-MM-dd") + "";
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View view) {
                    }
                })
                .build();
        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.5f);
            }
        }
    }


    public void setCompleteListener (CompleteListener completeListener) {
        mCompleteListener = completeListener;
    }

    public void showPopupWindow () {
        pvTime.show();
    }

    public interface CompleteListener {
        void onCompleteClick (String timeLong);
    }
}
